import type { PayParams, UploadImageResponse, UserData } from '@/types/user'
import { request } from '@/utils/request'

//login登录register注册changeMobile更换手机号forgetPassword找回密码,bindMobile绑定三方登录，区分验证码
interface GetCodeParams {
  mobile: string
  type: 'login' | 'register' | 'changeMobile' | 'forgetPassword' | 'bindMobile'
}

interface GetCodeResponse {
  code: string
}

// 获取验证码
export const getCodeApi = (params: GetCodeParams) => {
  return request.get<any, GetCodeResponse>('/code', {
    params,
  })
}

// 登录
interface LoginParams {
  mobile: string
  code: string
}

interface LoginResponse {
  token: string
  refreshToken: string
  avatar: string
  mobile: string
  account: string
  id: string
}

export const loginApi = (data: LoginParams) => {
  return request.post<any, LoginResponse>('/login', data)
}

//密码登录
interface PasswordLoginParams {
  mobile: string
  password: string
}

export const loginByPasswordApi = (data: PasswordLoginParams) => {
  return request.post<any, LoginResponse>('/login/password', data)
}

// 获取用户信息
export const getUserInfoApi = () => {
  return request.get<any, UserData>('/patient/myUser')
}

//上传图片
export const uploadImageApi = (uri: string) => {
  return request.upload<null, UploadImageResponse>('/upload', {
    name: 'file', // 服务器的 key
    filePath: uri, // 上传文件资源的路径(沙箱路径)，上传时会自动转换成二进制
  })
}

//上传图片2
// export const uploadImage2Api = (uri: string) => {
//   return uni.uploadFile<UploadImageResponse>({
//     url: 'https://consult-api.itheima.net/upload',
//     filePath: uri,
//     name: 'file',
//   })
// }

/**
 * 三方支付（暂时只支持支付宝支付）
 */
export const orderPayApi = (data: PayParams) => {
  return request.post<null, { payUrl: string }>('/patient/consult/pay', data)
}
